package com.hongxun.budget.ui.fragment.sg;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.widget.ImageView;
import android.widget.TextView;

import com.hongxun.budget.R;
import com.hongxun.budget.ui.activity.sg.ScheduleReportListActivity;
import com.hongxun.budget.ui.dialog.NoticeDialog;
import com.hongxun.budget.ui.fragment.BaseListFragment;
import com.hongxun.budget.utils.DateUtils;
import com.hongxun.budget.utils.RecyclerViewUtils;
import com.hongxun.budget.utils.SPUtils;
import com.hongxun.budget.utils.SgDBUtils;
import com.hongxun.budget.utils.SgStrKey;
import com.hongxun.budget.utils.XUtils;
import com.hongxun.greenentity.sg.CScheduleEntity;
import com.hongxun.interfaces.ClickListener;
import com.jessewu.library.SuperAdapter;
import com.jessewu.library.view.ViewHolder;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * 项目进度2级界面
 */
public class ProjectProgressList extends BaseListFragment {
    private SuperAdapter<CScheduleEntity> superAdapter;
    private List<CScheduleEntity> datas = new ArrayList<>();
    private Map<String, Integer> map = new HashMap<>();

    @Override
    public RecyclerView.LayoutManager getLayoutManager() {
        return RecyclerViewUtils.getLinearLayoutManagerForVertical(context);
    }

    @Override
    public SuperAdapter getAdapter() {
        superAdapter = new SuperAdapter<CScheduleEntity>(R.layout.sg_content_cs_pro_adapter) {
            @Override
            public void bindView(ViewHolder viewHolder, final CScheduleEntity itemBean, int i) {
                try {
                    ImageView imageView = viewHolder.getView(R.id.iv1);
                    Object ssUrl = SgDBUtils.getJdUrl(itemBean.getGuid());
                    XUtils.loadImg(context, ssUrl, imageView);
                    ((TextView) viewHolder.getView(R.id.tv1)).setText(itemBean.getSName());
                    ((TextView) viewHolder.getView(R.id.tv2)).setText(map.get(itemBean.getGuid()) + "");
                    Date inverTime = itemBean.getInverTime();
                    if (inverTime != null && inverTime.getTime() != 0) {
                        ((TextView) viewHolder.getView(R.id.tv3)).setText(DateUtils.getDateToStringNoHH(inverTime));
                    }
                    Date sTime = itemBean.getSTime();
                    if (sTime != null && sTime.getTime() != 0) {
                        ((TextView) viewHolder.getView(R.id.tv4)).setText(DateUtils.getDateToString(sTime));
                    }
                } catch (NullPointerException e) {
                }
            }
        };
        superAdapter.setData(datas);
        superAdapter.setEmptyDataView(R.layout.view_empty);
        superAdapter.setOnItemClickListener(new SuperAdapter.OnItemClickListener<CScheduleEntity>() {
            @Override
            public void onClick(int i, CScheduleEntity jdcj) {
                SPUtils.put(context, SgStrKey.uuidjd, jdcj.getGuid());
                SPUtils.put(context, SgStrKey.namejd, jdcj.getSName());
                Date inverTime = jdcj.getInverTime();
                if (inverTime != null && inverTime.getTime() != 0) {
                    SPUtils.put(context, SgStrKey.dpgqtime, DateUtils.getDateToStringNoHH(inverTime));
                } else {
                    SPUtils.put(context, SgStrKey.dpgqtime, "");
                }
                startActivity(new Intent(context, ScheduleReportListActivity.class));
            }
        });
        superAdapter.setOnItemLongClickListener(new SuperAdapter.OnItemLongClickListener<CScheduleEntity>() {
            @Override
            public void onLongClick(int i, CScheduleEntity o) {
                longClick(o);
            }
        });
        return superAdapter;
    }

    private void longClick(final CScheduleEntity o) {
        new NoticeDialog()
                .setTitle("删除此进度")
                .setMsg(o.getSName())
                .setCancelButton("取消", null)
                .setConfirmButton("删除", new ClickListener() {
                    @Override
                    public void click() {
                        datas.remove(o);
                        superAdapter.notifyDataSetChanged();
                        SgDBUtils.delCScheduleEntity(o.getGuid());
                    }
                }).show(getFragmentManager(), "");
    }

    @Override
    public void onRefresh() {
        datas.clear();
        Observable.just(SgStrKey.uuidxm).subscribeOn(Schedulers.io()).map(new Func1<String, Boolean>() {
            @Override
            public Boolean call(String key) {
                datas.addAll(SgDBUtils.getScheduleAll(String.valueOf(SPUtils.get(context, key, ""))));
                for (CScheduleEntity data : datas) {
                    map.put(data.getGuid(), SgDBUtils.getCSrListAll(data.getGuid()).size());
                }
                return true;
            }
        }).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<Boolean>() {
            @Override
            public void call(Boolean b) {
                superAdapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        onRefresh();
    }
}
